<template>
  <el-dialog
    v-model="dialogFormVisible"
    :title="title"
    width="500px"
    @close="close"
  >
    <el-form ref="formRef" label-width="80px" :model="form" :rules="rules">
      <el-form-item v-if="level == 0" label="楼幢名" prop="BuildName">
        <el-input v-model="form.BuildName" />
      </el-form-item>
      <el-form-item v-if="level == 1" label="单元名" prop="BuildUnit">
        <el-input v-model="form.BuildUnit" />
      </el-form-item>
      <el-form-item v-if="level == 2" label="楼层名" prop="FloorName">
        <el-input v-model="form.FloorName" />
      </el-form-item>
      <el-form-item v-if="level == 3" label="房间号" prop="RoomName">
        <el-input v-model="form.RoomName" />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="close">取 消</el-button>
      <el-button type="primary" @click="save">确 定</el-button>
    </template>
  </el-dialog>
</template>

<script>
  import {
    buildingEdit,
    BuildUnitEdit,
    floorEdit,
    roomEdit,
  } from '@/api/community'

  export default defineComponent({
    name: 'CommunityBuildingEdit',
    emits: ['fetch-data'],
    setup(props, { emit }) {
      const $baseMessage = inject('$baseMessage')

      const state = reactive({
        level: 0,
        formRef: null,
        form: {
          roles: [],
        },
        rules: {
          title: [{ required: true, trigger: 'blur', message: '请输入标题' }],
        },
        title: '',
        dialogFormVisible: false,
      })

      const showEdit = (row, num) => {
        state.level = num
        if (num == 0) {
          if (!row.BuildID) {
            state.title = '添加'
          } else {
            state.title = '编辑'
          }
        }
        if (num == 1) {
          if (!row.BuildUnitID) {
            state.title = '添加'
          } else {
            state.title = '编辑'
          }
        }
        if (num == 2) {
          if (!row.FloorID) {
            state.title = '添加'
          } else {
            state.title = '编辑'
          }
        }
        if (num == 3) {
          if (!row.RoomID) {
            state.title = '添加'
          } else {
            state.title = '编辑'
          }
        }

        state.form = Object.assign({}, row)
        state.dialogFormVisible = true
      }
      const close = () => {
        state['formRef'].resetFields()
        state.form = {
          roles: [],
        }
        state.dialogFormVisible = false
      }
      const save = async () => {
        state['formRef'].validate(async (valid) => {
          if (valid) {
            if (state.level == 0) {
              await isBuild()
            }
            if (state.level == 1) {
              await isBuildUnit()
            }
            if (state.level == 2) {
              await isFloor()
            }
            if (state.level == 3) {
              await isRoom()
            }
            emit('fetch-data')
            close()
          }
        })
      }

      const isBuild = async () => {
        const dataMsg = {
          BuildID: state.form.BuildID,
          BuildName: state.form.BuildName,
        }
        const { msg } = await buildingEdit(dataMsg)
        $baseMessage(msg, 'success', 'vab-hey-message-success')
      }
      const isBuildUnit = async () => {
        const dataMsg = {
          BuildUnitID: state.form.BuildUnitID,
          BuildUnit: state.form.BuildUnit,
        }
        const { msg } = await BuildUnitEdit(dataMsg)
        $baseMessage(msg, 'success', 'vab-hey-message-success')
      }
      const isFloor = async () => {
        const dataMsg = {
          FloorID: state.form.FloorID,
          FloorName: state.form.FloorName,
        }
        const { msg } = await floorEdit(dataMsg)
        $baseMessage(msg, 'success', 'vab-hey-message-success')
      }
      const isRoom = async () => {
        const dataMsg = {
          BuildID: state.form.BuildID,
          BuildUnitID: state.form.BuildUnitID,
          FloorID: state.form.FloorID,
          RoomID: state.form.RoomID,
          RoomName: state.form.RoomName,
        }
        const { msg } = await roomEdit(dataMsg)
        $baseMessage(msg, 'success', 'vab-hey-message-success')
      }

      return {
        ...toRefs(state),
        showEdit,
        isBuild,
        isBuildUnit,
        isFloor,
        isRoom,
        close,
        save,
      }
    },
  })
</script>
